package com.c.yinyuezhushou.OnScrollListener;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.Fragment.LyricsFragment;
import com.c.yinyuezhushou.R;



public class LyricRecOnScrollListener extends RecyclerView.OnScrollListener{
    private LyricsFragment lyricsFragment;
    private boolean is=false;
    private TextView textView;
    private LinearLayoutManager linearLayoutManager;


    public LyricRecOnScrollListener(LyricsFragment lyricsFragment, LinearLayoutManager linearLayoutManager){
        this.lyricsFragment = lyricsFragment;
        this.linearLayoutManager = linearLayoutManager;
    }

    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        if (newState == 1){
            lyricsFragment.setSliding(true);
            is = true;
            lyricsFragment.getRelativeLayout().setVisibility(View.VISIBLE);
        }
        if (newState ==0){
            is = false;
            handler.sendEmptyMessageDelayed(0,3000);
        }

    }



    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what==0){
                if (!is){
                    lyricsFragment.setSliding(false);
                    lyricsFragment.getRelativeLayout().setVisibility(View.GONE);
                    if (textView!=null){
                        textView.setTextSize(15);
                        textView.setTextColor(0x86FFFFFF);
                    }
                }
            }else{
                removeMessages(0);
            }

        }
    };

    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (lyricsFragment.isSliding()){
            if (textView!=null){
                textView.setTextSize(15);
                textView.setTextColor(lyricsFragment.getResources().getColor(R.color.bai1));
            }
            int index = lyricsFragment.getBootom()-lyricsFragment.getCz()+1;
            if (index<0){
                index=0;
            }
            setPro(index);
            setText(index);
        }
    }

    public void setText(int i){
        if (linearLayoutManager.findViewByPosition(i)!=null){
            textView = linearLayoutManager.findViewByPosition(i).findViewById(R.id.ilr_text);
            textView.setTextSize(18);
            textView.setTextColor(lyricsFragment.getResources().getColor(R.color.bai));
        }
    }

    public void setPro(int l){
        if (l<0){
            l=0;
        }
        lyricsFragment.getProgressBar().setProgress(lyricsFragment.getList().get(l)
                .intValue());
    }

    public void closehandle(){
        handler.sendEmptyMessageDelayed(1,0);
    }

}
